# @Author   : 懒羊羊
# @File     : day05_file.py
# @Time     : 2024/11/29 11:04
import json
import os

import openpyxl
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

if __name__ == '__main__':
    # 读写文件
    # 打开文件函数:open（）
    from config.config import data_path

    #
    # file = os.path.join(data_path, "1.txt")
    # f = open(file, mode='r', encoding='utf-8')
    #
    # # readlines 返回列表形式的数据
    # # my_list = f.readlines()
    #
    # # 返回第一行数据
    # # my_list = f.readline()
    #
    # strs = f.read()
    # print(strs)
    # f.close()
    # # print(my_list)
    #
    # # 写  如果文件存在，先清空再写，如果文件不存在，先创建再写
    # file2 = os.path.join(data_path, 'test.txt')
    # f = open(file2, mode='w', encoding='utf-8')
    # str2 = f.write("我在写东西")
    # f.close()

    # 读图片
    # file3 = os.path.join(data_path, "monkey.png")
    # f = open(file3, mode='rb')
    # strs = f.read()
    # print(strs)
    # f.close()

    # file5 = os.path.join(data_path, "1.txt")
    # with open(file5, mode='r', encoding='utf-8') as f:
    #     strs = f.read()
    #     print(strs)
    #
    # # 写  如果文件存在，先清空再写，如果文件不存在，先创建再写
    # file6 = os.path.join(data_path, 'test.txt')
    # with open(file6, mode='w', encoding='utf-8') as f:
    #     str2 = f.write("我在写东西")

    # 将字典转为json文件
    my_dict = {
        "运单号": "500367891209",
        "收货人": "王五",
        "联系电话": "13520316789",
        "message": "获取成功",
        "收货地址": "海南三亚河道边"
    }

    file5 = os.path.join(data_path, "my_json1.json")
    with open(file5, mode='w', encoding='utf-8') as f:
        json.dump(my_dict, f, ensure_ascii=False)

    # 将json文件转为字典
    with open(file5, mode='r', encoding='utf-8') as f:
        dd = json.load(f)

    print(dd)

    # 处理excel，需要下载第三方库 openpyxl

    # 读excel文件
    # efile = os.path.join(data_path, "测试用例练习.xlsx")
    # # 载入一个工作簿
    # workbook: Workbook = openpyxl.load_workbook(efile)
    # 获取要处理的sheet页
    # 变量名：类型   是对变量的注解
    # sheet1: Worksheet = workbook.get_sheet_by_name("11.5作业")
    # 获取当前激活的sheet
    # sheet1: Worksheet = workbook.active
    # a = sheet1.iter_rows(2, 6, 2, 4, values_only=True)
    # for i in a:
    #     print(i)

    # 创建excel并写
    # wk = openpyxl.Workbook()
    # st:Worksheet = wk.create_sheet("st1")
    # st.cell(1, 1, "aaa")
    # st.cell(1, 2, "bbb")
    # efiles = os.path.join(data_path, "pyExcelCreat.xlsx")
    # wk.save(efiles)


